package cn.eeepay.framework.dao;

import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.JumpRouteQuotaLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;


public interface JumpRouteQuotaLogDao {


    @Insert("insert into jump_route_quota_log(jump_id,remark,target_quota,group_code,account_date,create_time) " +
            "values(#{jumpId},#{remark},#{targetQuota},#{groupCode},#{accountDate},#{createTime})")
    int insert(JumpRouteQuotaLog jumpRouteQuotaLog);

    @SelectProvider(type = SqlProvider.class, method = "routeMonitorLogPage")
    @ResultType(JumpRouteQuotaLog.class)
    List<JumpRouteQuotaLog> routeMonitorLogPage(@Param("jumpId") Integer jumpId, @Param("today") String today, @Param("page") Page<JumpRouteQuotaLog> page);

    @SelectProvider(type = SqlProvider.class, method = "routeMonitorHistoryLogPage")
    @ResultType(JumpRouteQuotaLog.class)
    List<JumpRouteQuotaLog> routeMonitorHistoryLogPage(@Param("jumpId") Integer jumpId, @Param("dateStart") String dateStart, @Param("dateEnd") String dateEnd, @Param("page") Page<JumpRouteQuotaLog> page);

    public class SqlProvider {
        public String routeMonitorLogPage(final Map<String, Object> param) {
            final Integer jumpId = (Integer) param.get("jumpId");
            return new SQL() {
                {
                    SELECT("l.id jump_id,SUM(t.trans_amount) trans_amount,l.remark,l.target_amount target_quota,t.account_date,t.group_code");
                    FROM("jump_route_config l left join account_addr_order t on t.group_code and l.group_code and t.jump_id = l.id");
                    if (jumpId != null && jumpId > 0) {
                        WHERE("t.jump_id = #{jumpId}");
                    }
                    WHERE("t.account_date = #{today}");
                    GROUP_BY("t.jump_id,t.group_code");
                    ORDER_BY("t.account_date desc");
                }
            }.toString();
        }


        public String routeMonitorHistoryLogPage(final Map<String, Object> param) {
            final Integer jumpId = (Integer) param.get("jumpId");
            return new SQL() {
                {
                    SELECT("t.account_date,t.jump_id,t.group_code,SUM(t.trans_amount) trans_amount,l.remark,target_quota");
                    FROM("jump_route_quota_log l left join account_addr_order t on l.account_date=t.account_date AND l.jump_id=t.jump_id and l.group_code = t.group_code");
                    if (jumpId != null && jumpId > 0) {
                        WHERE("t.jump_id = #{jumpId}");
                    }
                    WHERE("t.account_date BETWEEN #{dateStart} and #{dateEnd}");
                    GROUP_BY("t.account_date,t.jump_id,t.group_code");
                    ORDER_BY("t.account_date desc");
                }
            }.toString();
        }

    }
}
